home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #44 (May 89) / Hyper Stuff / XCmdGlue.c < prev   
Text File  |  1989-02-27  |  13KB  |  557 lines

  1. /************************************/
  2. /* File: XCMDGlue.c                    */
  3. /*                                    */
  4. /* Callback routines for XCMDs        */
  5. /* and XFCNs.  This file should     */
  6. /* be included in your project        */
  7. /*                                    */
  8. /* Based on original work by        */
  9. /* Dan Winkler of Apple Computer    */
  10. /*                                    */
  11. /************************************/
  12.  
  13.  
  14. #include    <MacTypes.h>
  15. #include    <OSUtil.h>
  16. #include    <MemoryMgr.h>
  17. #include    <FileMgr.h>
  18. #include    <ResourceMgr.h>
  19. #include     "HyperXCmd.h"
  20. #include    <math.h>
  21.  
  22.  
  23. pascal void SendCardMessage(paramPtr,msg)
  24.     XCmdBlockPtr    paramPtr;    
  25.     StringPtr        msg;
  26. /***********************
  27. * Send a message back to 
  28. * hypercard.  The input message
  29. * is a Pascal String
  30. ***********************/
  31. {
  32.     paramPtr->inArgs[0] = (long)msg;
  33.     paramPtr->request     = xreqSendCardMessage;
  34.     CallPascal( paramPtr->entryPoint );
  35. }
  36.  
  37.  
  38. pascal Handle EvalExpr(paramPtr,expr)
  39.     XCmdBlockPtr    paramPtr;    
  40.     StringPtr        expr;
  41. /***********************
  42. * Evaluate a Hypertalk expression
  43. * returning the result as a "C" 
  44. * string
  45. ***********************/
  46. {
  47.     paramPtr->inArgs[0] = (long)expr;
  48.     paramPtr->request     = xreqEvalExpr;
  49.     CallPascal( paramPtr->entryPoint );
  50.     return (Handle)paramPtr->outArgs[0];
  51. }
  52.  
  53. pascal long StringLength(paramPtr,strPtr)
  54.     XCmdBlockPtr    paramPtr;
  55.     StringPtr        strPtr;
  56. /***********************
  57. * Counts the number of 
  58. * characters in the input 
  59. * string from StrPtr to end
  60. * of string (zero byte)
  61. ***********************/
  62. {
  63.     paramPtr->inArgs[0] = (long)strPtr;
  64.     paramPtr->request     = xreqStringLength;
  65.     CallPascal( paramPtr->entryPoint );
  66.     return (long)paramPtr->outArgs[0];
  67. }
  68.  
  69.  
  70. pascal Ptr StringMatch(paramPtr,pattern,target)
  71.     XCmdBlockPtr    paramPtr;    
  72.     StringPtr        pattern;
  73.     Ptr                target;
  74. /***********************
  75. * Case-insensitive match 
  76. * for pattern anywhere in
  77. * target, 
  78. * Returns a pointer to first
  79. * character of the first match,
  80. * in target or NIL if no match
  81. * found.  pattern is a Pascal string,
  82. * and target is a zero-terminated string.
  83. ***********************/
  84. {
  85.     paramPtr->inArgs[0] = (long)pattern;
  86.     paramPtr->inArgs[1] = (long)target;
  87.     paramPtr->request     = xreqStringMatch;
  88.     CallPascal( paramPtr->entryPoint );
  89.     return (Ptr)paramPtr->outArgs[0];
  90. }
  91.  
  92.  
  93. pascal void ZeroBytes(paramPtr,dstPtr,longCount)
  94.     XCmdBlockPtr    paramPtr;    
  95.     Ptr                dstPtr;    
  96.     long            longCount;
  97. /***********************
  98. * Clear memory starting at destPtr
  99. * through destPtr+longCount
  100. ***********************/
  101. {
  102.     paramPtr->inArgs[0] = (long)dstPtr;
  103.     paramPtr->inArgs[1] = longCount;
  104.     paramPtr->request     = xreqZeroBytes;
  105.     CallPascal( paramPtr->entryPoint );
  106. }
  107.  
  108.  
  109. pascal Handle PasToZero(paramPtr,pasStr)
  110.     XCmdBlockPtr    paramPtr;
  111.     StringPtr        pasStr;
  112. /***********************
  113. * Convert a Pascal string (STR255)
  114. * to a zero-terminated string.  
  115. * Returns a handle to a zero-terminated
  116. * string.  The caller must dispose the handle.
  117. *
  118. * Useful for setting the result or
  119. * an argument you send from 
  120. * an XCMD to HyperTalk.
  121. ***********************/
  122. {
  123.     paramPtr->inArgs[0] = (long)pasStr;
  124.     paramPtr->request     = xreqPasToZero;
  125.     CallPascal( paramPtr->entryPoint );
  126.     return (Handle)paramPtr->outArgs[0];
  127. }
  128.  
  129. pascal void ZeroToPas(paramPtr,zeroStr,pasStr)
  130.     XCmdBlockPtr    paramPtr;    
  131.     char            *zeroStr;    
  132.     StringPtr        pasStr;
  133. /***********************
  134. * Copy the zero-terminated
  135. * string into the Pascal String.
  136. *
  137. * You create the Pascal string 
  138. * and pass it by reference.
  139. *
  140. ***********************/
  141. {
  142.     paramPtr->inArgs[0] = (long)zeroStr;
  143.     paramPtr->inArgs[1] = (long)pasStr;
  144.     paramPtr->request     = xreqZeroToPas;
  145.     CallPascal( paramPtr->entryPoint );
  146. }
  147.  
  148.  
  149. pascal long StrToLong(paramPtr,strPtr)
  150.     XCmdBlockPtr    paramPtr;    
  151.     Str31             *strPtr;
  152. /***********************
  153. * Convert a string of ASCII
  154. * characters to an unsigned 
  155. * long integer.
  156. ***********************/
  157. {
  158.     paramPtr->inArgs[0] = (long)strPtr;
  159.     paramPtr->request     = xreqStrToLong;
  160.     CallPascal( paramPtr->entryPoint );
  161.     return (long)paramPtr->outArgs[0];
  162. }
  163.  
  164. pascal long StrToNum(paramPtr,str)
  165.     XCmdBlockPtr    paramPtr;    
  166.     Str31             *str;
  167. /***********************
  168. * Convert a string of ASCII
  169. * characters to a signed 
  170. * long integer.
  171. ***********************/
  172. {
  173.     paramPtr->inArgs[0] = (long)str;
  174.     paramPtr->request     = xreqStrToNum;
  175.     CallPascal( paramPtr->entryPoint );
  176.     return paramPtr->outArgs[0];
  177. }
  178.  
  179.  
  180. pascal Boolean StrToBool(paramPtr,str)
  181.     XCmdBlockPtr    paramPtr;
  182.     Str31             *str;
  183. /***********************
  184. * Convert the Pascal strings
  185. * 'true' and 'false' to booleans.
  186. ***********************/
  187. {
  188.     paramPtr->inArgs[0] = (long)str;
  189.     paramPtr->request     = xreqStrToBool;
  190.     CallPascal( paramPtr->entryPoint );
  191.     return (Boolean)paramPtr->outArgs[0];
  192. }
  193.  
  194.  
  195. pascal void StrToExt(paramPtr,str,myext)
  196.     XCmdBlockPtr    paramPtr;
  197.     Str31             *str;    
  198.     long             *myext;
  199. /***********************
  200. * Convert a string of ASCII digits 
  201. * to an extended long integer.
  202. *
  203. * The return value is passed
  204. * by reference and you must
  205. * asllocate the space before 
  206. * calling this routine.
  207. ***********************/
  208. {
  209.     paramPtr->inArgs[0] = (long)str;
  210.     paramPtr->inArgs[1] = (long)myext;
  211.     paramPtr->request     = xreqStrToExt;
  212.     CallPascal( paramPtr->entryPoint );
  213. }
  214.  
  215.  
  216. pascal void LongToStr(paramPtr,posNum,mystr)
  217.     XCmdBlockPtr    paramPtr;    
  218.     long             posNum;    
  219.     Str31             *mystr;
  220. /***********************
  221. * Convert an unsigned long integer
  222. * to a pascal string representation
  223. * Useful for sending numbers back 
  224. * to Hypercard.
  225. *
  226. *  You create mystr and pass
  227. * it by reference.
  228. ***********************/
  229. {
  230.     paramPtr->inArgs[0] = (long)posNum;
  231.     paramPtr->inArgs[1] = (long)mystr;
  232.     paramPtr->request     = xreqLongToStr;
  233.     CallPascal( paramPtr->entryPoint );
  234. }
  235.  
  236.  
  237. pascal void NumToStr(paramPtr,num,mystr)
  238.     XCmdBlockPtr    paramPtr;    
  239.     long             num;    
  240.     Str31             *mystr;
  241. /***********************
  242. * Convert a signed long integer
  243. * to a pascal string representation
  244. * Useful for sending numbers back 
  245. * to Hypercard.
  246. *
  247. *  You create mystr and pass
  248. * it by reference.
  249. ***********************/
  250. {
  251.     paramPtr->inArgs[0] = num;
  252.     paramPtr->inArgs[1] = (long)mystr;
  253.     paramPtr->request     = xreqNumToStr;
  254.     CallPascal( paramPtr->entryPoint );
  255. }
  256.  
  257.  
  258. pascal void NumToHex(paramPtr,num,nDigits,mystr)
  259.     XCmdBlockPtr    paramPtr;    
  260.     long             num;
  261.     short            nDigits;            
  262.     Str31             *mystr;
  263. /***********************
  264. * Convert an unsigned long integer
  265. * to a hexadecimal number and put it
  266. * into a Pascal string.
  267. *
  268. * The "output" string is passed
  269. * by reference.
  270. ***********************/
  271. {
  272.     paramPtr->inArgs[0] = num;
  273.     paramPtr->inArgs[1] = nDigits;
  274.     paramPtr->inArgs[2] = (long)mystr;
  275.     paramPtr->request     = xreqNumToHex;
  276.     CallPascal( paramPtr->entryPoint );
  277. }
  278.  
  279.  
  280. pascal void BoolToStr(paramPtr,bool,mystr)
  281.     XCmdBlockPtr    paramPtr;    
  282.     Boolean            bool;    
  283.     Str31             *mystr;
  284. /***********************
  285. * Convert a boolean to 
  286. * 'true' or 'false'.  
  287. *
  288. * The "output" string is passed
  289. * by reference.
  290. ***********************/
  291. {
  292.     paramPtr->inArgs[0] = (long)bool;
  293.     paramPtr->inArgs[1] = (long)mystr;
  294.     paramPtr->request     = xreqBoolToStr;
  295.     CallPascal( paramPtr->entryPoint );
  296. }
  297.  
  298.  
  299. pascal void ExtToStr( paramPtr, myext, mystr)
  300.     XCmdBlockPtr    paramPtr;    
  301.     char             *myext;    
  302.     Str31             *mystr;
  303. /***********************
  304. * Convert an extended long
  305. * to its string representation
  306. *
  307. * The "output" string is passed
  308. * by reference.
  309. ***********************/
  310. {
  311.     paramPtr->inArgs[0] = (long)myext;
  312.     paramPtr->inArgs[1] = (long)mystr;
  313.     paramPtr->request     = xreqExtToStr;
  314.     CallPascal( paramPtr->entryPoint );
  315. }
  316.  
  317.  
  318. pascal Handle GetGlobal(paramPtr,globName)
  319.     XCmdBlockPtr    paramPtr;    
  320.     StringPtr        globName;
  321. /***********************
  322. * Return a handle to a zero-terminated
  323. * string containing the value of 
  324. * the specified HyperTalk global variable.
  325. ***********************/
  326. {
  327.     paramPtr->inArgs[0] = (long)globName;
  328.     paramPtr->request     = xreqGetGlobal;
  329.     CallPascal( paramPtr->entryPoint );
  330.     return (Handle)paramPtr->outArgs[0];
  331. }
  332.  
  333. pascal void SetGlobal(paramPtr,globName,globValue)
  334.     XCmdBlockPtr    paramPtr;    
  335.     StringPtr        globName;    
  336.     Handle            globValue;
  337. /***********************
  338. * Set the value of the specified 
  339. * HyperTalk global variable to be
  340. * the zero-terminated string in globValue.
  341. * The contents of globValue
  342. * are copied, you dispose the
  343. * handle
  344. ***********************/
  345. {
  346.     paramPtr->inArgs[0] = (long)globName;
  347.     paramPtr->inArgs[1] = (long)globValue;
  348.     paramPtr->request     = xreqSetGlobal;
  349.     CallPascal( paramPtr->entryPoint );
  350. }
  351.  
  352.  
  353. pascal Handle GetFieldByName(paramPtr,cardFieldFlag,fieldName)
  354.     XCmdBlockPtr    paramPtr;    
  355.     Boolean            cardFieldFlag;
  356.     StringPtr        fieldName;
  357. /***********************
  358. * Return a handle to a zero-terminated
  359. * string containing the value of 
  360. * field fieldName on the current 
  361. * card.  You must dispose the handle.
  362. *
  363. * Set cardfieldFlag to ture if
  364. * you want the contents of a card
  365. * field or to false if you want
  366. * the contents of a bkgnd field
  367. ***********************/
  368. {
  369.     paramPtr->inArgs[0] = (long)cardFieldFlag;
  370.     paramPtr->inArgs[1] = (long)fieldName;
  371.     paramPtr->request     = xreqGetFieldByName;
  372.     CallPascal( paramPtr->entryPoint );
  373.     return (Handle)paramPtr->outArgs[0];
  374. }
  375.  
  376.  
  377. pascal Handle GetFieldByNum(paramPtr,cardFieldFlag,fieldNum)
  378.     XCmdBlockPtr    paramPtr;    
  379.     Boolean            cardFieldFlag;
  380.     short            fieldNum;
  381. /***********************
  382. * Returns a copy of the contents
  383. * of the field whose number is
  384. * fieldnum on the current
  385. * card.
  386. *
  387. * You dispose the handle when
  388. * you are done.
  389. ***********************/
  390. {
  391.     paramPtr->inArgs[0] = (long)cardFieldFlag;
  392.     paramPtr->inArgs[1] = fieldNum;
  393.     paramPtr->request     = xreqGetFieldByNum;
  394.     CallPascal( paramPtr->entryPoint );
  395.     return (Handle)paramPtr->outArgs[0];
  396. }
  397.  
  398.  
  399. pascal Handle GetFieldByID(paramPtr,cardFieldFlag,fieldID)
  400.     XCmdBlockPtr    paramPtr;    
  401.     Boolean            cardFieldFlag;
  402.     short            fieldID;
  403. /***********************
  404. * Returns a copy of the contents
  405. * of the field whose id is
  406. * fieldID on the current
  407. * card.
  408. *
  409. * You dispose the handle when
  410. * you are done.
  411. ***********************/
  412. {
  413.     paramPtr->inArgs[0] = (long)cardFieldFlag;
  414.     paramPtr->inArgs[1] = fieldID;
  415.     paramPtr->request     = xreqGetFieldByID;
  416.     CallPascal( paramPtr->entryPoint );
  417.     return (Handle)paramPtr->outArgs[0];
  418. }
  419.  
  420.  
  421. pascal void SetFieldByName(paramPtr,cardFieldFlag,fieldName,fieldVal)
  422.     XCmdBlockPtr    paramPtr;    
  423.     Boolean            cardFieldFlag;
  424.     StringPtr        fieldName;    
  425.     Handle            fieldVal;
  426. /***********************
  427. * Set the value of the field 
  428. * whose name is fieldName on
  429. * the current card.
  430. *
  431. * You dispose the handle when
  432. * you are done.
  433. ***********************/
  434. {
  435.     paramPtr->inArgs[0] = (long)cardFieldFlag;
  436.     paramPtr->inArgs[1] = (long)fieldName;
  437.     paramPtr->inArgs[2] = (long)fieldVal;
  438.     paramPtr->request     = xreqSetFieldByName;
  439.     CallPascal( paramPtr->entryPoint );
  440. }
  441.  
  442.  
  443. pascal void SetFieldByNum(paramPtr,cardFieldFlag,fieldNum,fieldVal)
  444.     XCmdBlockPtr    paramPtr;    
  445.     Boolean            cardFieldFlag;
  446.     short            fieldNum;            
  447.     Handle            fieldVal;
  448. /***********************
  449. * Set the value of the field 
  450. * whose number is fieldnum on
  451. * the current card.
  452. *
  453. * You dispose the handle when
  454. * you are done.
  455. ***********************/
  456. {
  457.     paramPtr->inArgs[0] = (long)cardFieldFlag;
  458.     paramPtr->inArgs[1] = fieldNum;
  459.     paramPtr->inArgs[2] = (long)fieldVal;
  460.     paramPtr->request     = xreqSetFieldByNum;
  461.     CallPascal( paramPtr->entryPoint );
  462. }
  463.  
  464.  
  465. pascal void SetFieldByID(paramPtr,cardFieldFlag,fieldID,fieldVal)
  466.     XCmdBlockPtr    paramPtr;    
  467.     Boolean            cardFieldFlag;
  468.     short            fieldID;            
  469.     Handle            fieldVal;
  470. /***********************
  471. * Set the value of the field 
  472. * whose id is fieldID on
  473. * the current card.
  474. *
  475. * You dispose the handle when
  476. * you are done.
  477. ***********************/
  478. {
  479.     paramPtr->inArgs[0] = (long)cardFieldFlag;
  480.     paramPtr->inArgs[1] = fieldID;
  481.     paramPtr->inArgs[2] = (long)fieldVal;
  482.     paramPtr->request     = xreqSetFieldByID;
  483.     CallPascal( paramPtr->entryPoint );
  484. }
  485.  
  486.  
  487. pascal Boolean StringEqual(paramPtr,str1,str2)
  488.     XCmdBlockPtr    paramPtr;    
  489.     Str31             *str1;    
  490.     Str31             *str2;
  491. /***********************
  492. * Returns true if the strings
  493. * match, false otherwise.
  494. * Compare is case insensitive
  495. ***********************/
  496. {
  497.     paramPtr->inArgs[0] = (long)str1;
  498.     paramPtr->inArgs[1] = (long)str2;
  499.     paramPtr->request     = xreqStringEqual;
  500.     CallPascal( paramPtr->entryPoint );
  501.     return (Boolean)paramPtr->outArgs[0];
  502. }
  503.  
  504.  
  505. pascal void ReturnToPas(paramPtr,zeroStr,pasStr)
  506.     XCmdBlockPtr    paramPtr;    
  507.     Ptr                zeroStr;    
  508.     StringPtr        pasStr;
  509. /***********************
  510. * Collect characters from zeroStr
  511. * to the next carriage Return and return 
  512. * them in the Pascal string pasStr. 
  513. * If no Return found, collect chars
  514. * until the end of the string (zero)
  515. ***********************/
  516. {
  517.     paramPtr->inArgs[0] = (long)zeroStr;
  518.     paramPtr->inArgs[1] = (long)pasStr;
  519.     paramPtr->request     = xreqReturnToPas;
  520.     CallPascal( paramPtr->entryPoint );
  521. }
  522.  
  523.  
  524. pascal void ScanToReturn(paramPtr,scanHndl)
  525.     XCmdBlockPtr    paramPtr;    
  526.     Ptr             *scanHndl;
  527. /***********************
  528. * Position the pointer, scanPtr,
  529. * at a Return character
  530. * or a zero byte. 
  531. ***********************/
  532. {
  533.     paramPtr->inArgs[0] = (long)scanHndl;
  534.     paramPtr->request     = xreqScanToReturn;
  535.     CallPascal( paramPtr->entryPoint );
  536. }
  537.  
  538.  
  539. pascal void ScanToZero(paramPtr,scanHndl)
  540.     XCmdBlockPtr    paramPtr;    
  541.     Ptr             *scanHndl;
  542. /***********************
  543. * Position the pointer, scanPtr,
  544. * at a  zero byte. 
  545. ***********************/
  546. {
  547.     paramPtr->inArgs[0] = (long)scanHndl;
  548.     paramPtr->request     = xreqScanToZero;
  549.     CallPascal( paramPtr->entryPoint );
  550. }
  551.  
  552.  
  553.  
  554.